package cn.edu.ncut.cs.springboot.petmanagementsystem.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

/**
 * 分页返回结果封装
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class PageResult<T> extends Result<List<T>> {

    private Long total;
    private Long size;
    private Long current;
    private Long pages;

    public PageResult() {
        super();
    }

    public PageResult(Integer current, Integer size) {
        super();
        this.current = current.longValue();
        this.size = size.longValue();
        this.setTimestamp(System.currentTimeMillis());
    }

    /**
     * 成功返回分页结果 - 使用 MyBatis Plus 的 IPage
     */
    public static <T> PageResult<T> success(IPage<T> page) {
        PageResult<T> result = new PageResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setData(page.getRecords());
        result.setTotal(page.getTotal());
        result.setSize(page.getSize());
        result.setCurrent(page.getCurrent());
        result.setPages(page.getPages());
        result.setTimestamp(System.currentTimeMillis());
        return result;
    }

    /**
     * 成功返回分页结果 - 手动设置分页数据
     */
    public static <T> PageResult<T> success(List<T> data, Long total, Long current, Long size) {
        PageResult<T> result = new PageResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setData(data);
        result.setTotal(total);
        result.setCurrent(current);
        result.setSize(size);
        result.setPages(calculatePages(total, size));
        result.setTimestamp(System.currentTimeMillis());
        return result;
    }

    /**
     * 空数据成功返回
     */
    public static <T> Result<T> success() {
        PageResult<T> result = new PageResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setData(null);
        result.setTotal(0L);
        result.setSize(0L);
        result.setCurrent(0L);
        result.setPages(0L);
        result.setTimestamp(System.currentTimeMillis());
        return (Result<T>) result;
    }

    /**
     * 错误返回
     */
    public static <T> PageResult<T> pageError(String message) {
        PageResult<T> result = new PageResult<>();
        result.setCode(ResultCode.ERROR.getCode());
        result.setMessage(message);
        result.setData(null);
        result.setTotal(0L);
        result.setSize(0L);
        result.setCurrent(0L);
        result.setPages(0L);
        result.setTimestamp(System.currentTimeMillis());
        return result;
    }

    /**
     * 计算总页数
     */
    private static Long calculatePages(Long total, Long size) {
        if (total == null || size == null || size == 0) {
            return 0L;
        }
        return (total + size - 1) / size;
    }

    /**
     * 是否有上一页
     */
    public boolean hasPrevious() {
        return current != null && current > 1;
    }

    /**
     * 是否有下一页
     */
    public boolean hasNext() {
        return pages != null && current != null && current < pages;
    }
}